package com.ztesoft.ossup.report.web.config;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ztesoft.ossup.report.domain.Report;
import com.ztesoft.ossup.report.service.DataPreviewService;
import com.ztesoft.ossup.report.web.BaseController;

@Controller
@RequestMapping("/report/configure/preview")
public class DataPreviewController extends BaseController{
	
	Logger log = Logger.getLogger(DataPreviewController.class);

	@Autowired
	DataPreviewService dataPreviewService;
	
	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public String preview(@PathVariable Long id, HttpServletRequest request) {
		request.setAttribute("reportId", id);
        return "/config/dataPreview";
    }
	
	@RequestMapping(value = "/data", method = RequestMethod.POST)
	@ResponseBody
    public com.ztesoft.ossup.report.domain.PageData getData(@RequestBody Map<String, Object> paramMap) {
		String reportId = paramMap.get("reportId").toString().replaceAll(",", "");
		int offset = (int) paramMap.get("offset");
		int limit = (int) paramMap.get("limit");
		
		//log.info("offset:"+offset+";limit:"+limit);//0 offset 10 limit
		Report report = dataPreviewService.getReportById(Long.parseLong(reportId));
		//String dataSql = "select * from report";
		//只需要截取20条来作为显示数据
		String dataSql = dataPreviewService.doSqlNoParams(report.getSql());
		
		com.ztesoft.ossup.report.domain.PageData pageData = dataPreviewService.getDataBySql(dataSql, offset, limit,20);
		
        return pageData;
    }
	
}
